package io.ziyt.mouse;

import java.io.IOException;
import java.net.*;
import java.util.Enumeration;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        //String string1 = "1232313";
        //replace(string1);
        //System.out.println(string1);
//		String string1 = "111";
//		String string2 = "222";
//
//		replace(string1, string2);
//		System.out.println(string1+"   "+string2);
//        MulticastChannel multicastChannelSend = new MulticastChannel(StandardProtocolFamily.INET);
//        multicastChannelSend.joinGroup(InetAddress.getByName("230.0.0.0"), NetworkInterface.getNetworkInterfaces().nextElement());
//        ScheduledExecutorService scheduledExecutorService= Executors.newScheduledThreadPool(5);
//        scheduledExecutorService.scheduleWithFixedDelay(()->{
//            multicastChannelSend.send(ByteBuffer.wrap("hello".getBytes()),new InetSocketAddress("230.0.0.0",4321));
//        },2,2, TimeUnit.SECONDS);
        getUp2();
//        getUp();
    }

    private static NetworkInterface getUp() throws SocketException {
        NetworkInterface target=null;
        Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
        while (enumeration.hasMoreElements()) {
            NetworkInterface networkInterface = enumeration.nextElement();
            Enumeration<InetAddress> addressEnumeration = networkInterface.getInetAddresses();
            while (addressEnumeration.hasMoreElements()){
                System.out.println(addressEnumeration.nextElement().toString());
            }
            if(!networkInterface.isLoopback()&&networkInterface.isUp()&&!networkInterface.isVirtual()){
                target=networkInterface;
            }
            System.out.println("====================="+networkInterface.isLoopback()+" "+networkInterface.isUp()+" "+networkInterface.isVirtual()+" "+networkInterface.getDisplayName());
        }
        return target;
    }

    private static NetworkInterface getUp2() throws SocketException {
        NetworkInterface target=null;
        Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
        while (enumeration.hasMoreElements()) {
            NetworkInterface networkInterface = enumeration.nextElement();
            List<InterfaceAddress> addressEnumeration = networkInterface.getInterfaceAddresses();
            for(InterfaceAddress inetSocketAddress:addressEnumeration){
                System.out.println(inetSocketAddress.getAddress()+" "+inetSocketAddress.getBroadcast());
            }
        }
        return target;
    }

    public static void replace(String a, String b) {
        String temp = "";

        temp = a;
        a = b;
        b = temp;
        //System.out.println(string.replace('1', '2'));;
        //System.out.println(string);
    }

    public static byte[] intToBytes(int value) {
        byte[] src = new byte[4];
        src[3] = (byte) ((value >> 24) & 0xFF);
        src[2] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[0] = (byte) (value & 0xFF);
        return src;
    }

    public static int bytesToInt(byte[] src, int offset) {
        int value;
        value = (int) ((src[offset] & 0xFF)
                | ((src[offset + 1] & 0xFF) << 8)
                | ((src[offset + 2] & 0xFF) << 16)
                | ((src[offset + 3] & 0xFF) << 24));
        return value;
    }
}
